import { App } from "vue";
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import {createRouterGuards} from './router-guards'

// 拼接路由对象
const getRoutes = (): RouteRecordRaw[] => {
	const routeModuleList: RouteRecordRaw[] = [];
	const modules: Record<string,any> = import.meta.globEager("./modules/*.ts");

	Object.keys(modules).forEach((key: string) => {
		const mod = modules[key].default || {};
		const modList = Array.isArray(mod) ? [...mod] : [mod];
		routeModuleList.push(...modList);
	});

	return routeModuleList;
};

const router = createRouter({
	routes: getRoutes(),
	history: createWebHistory(),
});

export function setupRouter(app: App) {
	app.use(router)
	// 创建路由守卫
	createRouterGuards(router)
}
export default router;